<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<style>
html,body{}
body,div{margin:0px;padding:0px;max-width:640px;margin:0px auto;}
.content{
position: relative;
width:100%;
margin:0px auto;
display:none;
background: transparent url('images/2.gif') no-repeat center top;background-size: cover;
}
.txt{position: relative;width:100%;margin:0px auto;background: transparent url('images/txt.png') no-repeat center top;background-size: cover;}
.header{
position: relative;
width:100%;
margin:0px auto;
display:none;
}
.htxt{position: relative;width:100%;margin:0px auto;}
.ling{
position: relative;
width:178px;
height:45px;
margin:0px auto;
}
.ling{position: relative;margin:0px auto;}
.ling a{width:100%;height:100%;display:block;}
</style>
</head>
<body><?php 
$card_id='pieCpuCSYcYc6pvanMRMtgcIScks';
$code='9552236';
include_once("../c/config.php");
require_once "jssdk.php";
$jssdk = new JSSDK($appId, $secret);
$signPackage = $jssdk->GetSignPackage();
$acToken=$jssdk->getAccessToken();
$apiTicket=json_decode(file_get_contents('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$acToken.'&type=wx_card'), true);
var_dump($apiTicket);
$api_ticket=$apiTicket['ticket'];

class Signature{
	function __construct(){
		$this->data = array();
	}
	function add_data($str){
		array_push($this->data, (string)$str);
	}
	function get_signature(){
		sort( $this->data, SORT_STRING );
		return sha1( implode( $this->data ) );
	}
};
$openid='oe2K2jjniX57VFTjLyP5wO7G3-to';
$timestamp=time();
$signature = new Signature();
$signature->add_data($api_ticket);
$signature->add_data($timestamp);
$signature->add_data($card_id);
$signature->add_data($code);
$signature->add_data($openid);
//$signature->add_data($signPackage["nonceStr"]);
$sign=$signature->get_signature();
 
?><div class="content">
      <div class="header"></div>
      <div class="txt"></div>
      <div class="htxt"></div>
      <div class="ling">
       <a class='jinru'>&nbsp;</a>
      </div> 
</div>
<script src="./js/jquery-1.9.1.min.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<script type="text/javascript">
	var sWidth = parseInt((window.innerWidth > 0) ? window.innerWidth : screen.width);
	if(sWidth>640){sWidth=640;}
	var _musicStatus='close';
	var _rate=(sWidth/640);
	$('.content').height(_rate*1008);
	$('.header').height(_rate*200);
	$('.txt').height(_rate*309);
	$('.htxt').height(_rate*90);
	$('.content').show();
	$('.header').show();
    $('.ling').width(_rate*460).height(_rate*150);
    
$(function(){

	 wx.config({
		    debug:true,
		    appId: '<?php echo $signPackage["appId"];?>',
		    timestamp: <?php echo $signPackage["timestamp"];?>,
		    nonceStr: '<?php echo $signPackage["nonceStr"];?>',
		    signature: '<?php echo $signPackage["signature"];?>',
		    jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'showMenuItems', 'hideMenuItems','addCard','chooseCard','openCard']
		  });
    
   
        $('.jinru').click(function(e) { 
        	 wx.addCard({
        	      cardList: [
        	        {
        	          cardId: '<?php echo $card_id; ?>',
        	          cardExt: '{"code": "<?php echo $code; ?>", "openid": "<?php echo $openid; ?>", "timestamp": "<?php echo $timestamp; ?>", "signature":"<?php echo  $sign; ?>"}'
        	        }
        	      ],
        	      success: function (res) {
        	        alert('已添加卡券：' + JSON.stringify(res.cardList));
        	      },
        	      cancel: function (res) {
        	        alert(JSON.stringify(res))
        	      }
        	    });
        });
    

});
</script>

</div>
</body>
</html>